#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Input;
using UTable.Input.MultiTouch;
using System.Windows;
using System.Windows.Controls;

namespace UTable.Objects.Controls
{
	public class UControlWrapper: UContentControl
	{
		private int _currentFingerID = -1;
		private UIElement _currentHitElement = null;

		public override void OnInputReceived(InputEventArgs args)
		{
			base.OnInputReceived(args);

			if (args is MultiTouchEventArgs)
			{
				OnMultiTouchInputReceived(args as MultiTouchEventArgs);
			}
		}

		public void OnMultiTouchInputReceived(MultiTouchEventArgs args)
		{
			// generate the mouse input event, and send it to the content

			foreach (FingerEventArgs finger in args.FingerEvents)
			{
				if (finger.FingerID == _currentFingerID)
				{
					switch (finger.EventType)
					{
						case FingerEventType.FINGER_OUT_EVENT:
							FingerUp(finger.Position);
							break;
						case FingerEventType.FINGER_UP_EVENT:
							FingerUp(finger.Position);
							break;
						case FingerEventType.FINGER_MOVE_EVENT:
							FingerMove(finger.Position);
							break;
					}
				}
				else
				{
					// only when there is no finger captured, then process the finger event
					if (_currentFingerID < 0)
					{
						switch (finger.EventType)
						{
							case FingerEventType.FINGER_DOWN_EVENT:
								FingerDown(finger.Position);
								break;
						}
					}
				}
			}
		}

		private void FingerDown(Point position)
		{
			System.Windows.Input.MouseButtonEventArgs e = new System.Windows.Input.MouseButtonEventArgs(
				System.Windows.Input.Mouse.PrimaryDevice, 0, System.Windows.Input.MouseButton.Left);
            // e.RoutedEvent = System.Windows.Input.Mouse.
		}

		private void FingerMove(Point position)
		{

		}

		private void FingerUp(Point position)
		{
            _currentFingerID = -1;
		}

		protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonDown(e);
			e.Handled = true;
		}

		protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonUp(e);
			e.Handled = true;
		}

		protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
		{
			base.OnMouseMove(e);
			e.Handled = true;
		}

		protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
		{
			base.OnMouseEnter(e);
			e.Handled = true;
		}
		protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
		{
			base.OnMouseLeave(e);
			e.Handled = true;
		}

		protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
		{
			base.OnMouseDown(e);
			e.Handled = true;
		}

		protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
		{
			base.OnMouseUp(e);
			e.Handled = true;
		}
	}
}
